package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntTaskInterviewMapper;
import com.prd.model.EntTaskInterview;
import com.prd.model.LoginUser;
import com.prd.service.EntTaskInterviewService;
import com.prd.service.UserService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntTaskInterviewServiceImpl extends ServiceImpl<EntTaskInterviewMapper, EntTaskInterview> implements EntTaskInterviewService {

    @Autowired
    private EntTaskInterviewMapper entTaskInterviewMapper;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntTaskInterview entTaskInterview) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entTaskInterview);
        entTaskInterview.setVerify(IsStatus.TWO.getCode());
        return entTaskInterviewMapper.insert(entTaskInterview);
    }

    @Override
    public int updateInfo(EntTaskInterview entTaskInterview) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entTaskInterview);
        return entTaskInterviewMapper.updateById(entTaskInterview);
    }

    @Override
    public List<EntTaskInterview> listInfo(EntTaskInterview entTaskInterview) {
        LambdaQueryWrapper<EntTaskInterview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entTaskInterview);
        List<EntTaskInterview> list = entTaskInterviewMapper.selectList(queryWrapper);
        for (EntTaskInterview interview : list) {
            interview.setInterviewee(userService.listName(Long.parseLong(interview.getInterviewee())).getMerge());
            interview.setPrincipal(userService.listName(Long.parseLong(interview.getPrincipal())).getMerge());
            interview.setParticipation(userService.listName(Long.parseLong(interview.getParticipation())).getMerge());
            interview.setCompere(userService.listName(Long.parseLong(interview.getCompere())).getMerge());
            interview.setNotekeeper(userService.listName(Long.parseLong(interview.getNotekeeper())).getMerge());
        }
        return list;
    }

    @Override
    public EntTaskInterview details(Long id) {
        return entTaskInterviewMapper.selectById(id);
    }
}
